package com.qiezikaka.kaka.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class CircularFrameLayout extends FrameLayout {
    private Path circularPath;
    private RectF rectF;

    public CircularFrameLayout(Context context) {
        super(context);
        init();
    }

    public CircularFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircularFrameLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        circularPath = new Path();
        rectF = new RectF();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectF.set(0, 0, w, h);
        circularPath.reset();
        circularPath.addOval(rectF, Path.Direction.CW);
        circularPath.close();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.save();
        canvas.clipPath(circularPath);
        super.dispatchDraw(canvas);
        canvas.restore();
    }
}
